<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加门店管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" />
    <link rel="stylesheet" href="/static/ui/css/ui.css" />
</head>

<body>
    <form class="layui-form" action="" lay-filter="editform">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">门店名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" lay-verify="required|len" autocomplete="off" min="3"
                                placeholder="请输入名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">权限列表</label>
                        <div class="layui-input-block">
                            <div id="treearea"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属公司</label>
                        <div class="layui-input-block">
                            <div id="company-area" class="xm-select-demo"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">所在地区</label>
                        <div class="layui-input-block">
                            <div id="area-area" class="xm-select-demo"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系人</label>
                        <div class="layui-input-block">
                            <input type="text" name="contact" autocomplete="off" placeholder="请输入公司联系人"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="number" name="tel" autocomplete="off" placeholder="请输入公司电话"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">门店地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" autocomplete="off" min="2" placeholder="请输入公司名称"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <input type="hidden" name="id" />
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="store-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
            </div>
        </div>
    </form>
    <script src="../config.js"></script>
    <script src="/static/layui/layui.js"></script>
    <script src="/static/ui/ui.js"></script>
    <script src="/static/utils/xm-select.js"></script>
    <script src="../utils/select.js"></script>
    <script>
        layui.use(['form', 'layer', 'tree'], function () {
            let form = layui.form
                , layer = layui.layer
                , tree = layui.tree
                , $ = layui.$
                , req = _req();
            form.verify({
                len: (val, item) => {
                    let min = item.getAttribute("min")
                    if (val.length < min) {
                        return '长度不能小于' + min;
                    }
                }
            })
            let companyIns, areaIns;
            const renderSelect = (res) => {
                tree.render({
                    elem: '#treearea'//绑定元素
                    , showCheckbox: true
                    , data: res.menus
                    , id: 'trees' //定义索引
                });
                companyIns = getElSelect(res.companys, res.company_id, 'company-area', 'radio', true, {name : 'company_id'});
                //areaIns = getElSelect(res.areaData, res.area_id, 'area-area', 'radio', true, {name : 'area_id'});
                areaIns = getAreaSelect('area-area', 'area_id', 'radio', res.area_id)
            }
            if (req.id) {
                //渲染
                _get(layui, 'store/editBefore?id=' + req.id, res => {
                    form.val('editform', res)
                    renderSelect(res)
                    if (res.rules.length > 0) {
                        tree.setDataChecked('trees', res.rules.split(','));
                        form.render('checkbox')
                    }
                });
            } else {
                _get(layui, 'store/addBefore', res => {
                    renderSelect(res)
                })
            }

            //监听提交
            form.on('submit(store-save)', function (data) {
                data = data.field;
                let arr = tree.getChecked('trees');
                data.rules = getTree(arr);
                
                //data.company_id = getSelectData(companyIns);
                //data.area_id = getSelectData(areaIns);

                let postUrl;
                if (req.id) {
                    postUrl = 'store/edit';
                    data.id = req.id;
                } else {
                    postUrl = 'store/add';
                }
                //console.log(data)
                //return false;
                _post(layui, postUrl, data, res => {
                    //console.log(res)
                    parent.layui.table.reload("store-table");
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页

                })

                return false;
            });

        });
    </script>

</body>

</html>